<template>
  <div>
    <el-form class="query-form" @submit.native.prevent :inline="true">
      <el-form-item>
        <select-address
          v-model="params.address_code"
          auto-top
          placeholder="所属区县"
          @change="getData()"
        ></select-address>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="params.customer_name"
          :clearable="true"
          placeholder="主体名称查询"
          @change="getData()"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="params.lender_idcard"
          :clearable="true"
          placeholder="身份证号码"
          @change="getData()"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-date-picker
          v-model="params.pay_time"
          type="month"
          value-format="yyyy-MM"
          placeholder="选择结息还本时间"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>

      <el-form-item class="float-right">
        <el-button
          type="primary"
          icon="el-icon-download"
          :loading="exportLoading"
          @click="handleDownload()"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      class="table-sm"
      v-loading="loading"
      cell-class-name="input-td"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
    >
      <template v-slot:interest="scope">
        <el-input v-model="scope.row.interest" type="number" @blur="submit(scope.row)"></el-input>
      </template>

      <template v-slot:repay_amount="scope">
        <el-input
          v-model="scope.row.repay_amount"
          type="number"
          @blur="submit(scope.row)"
        ></el-input>
      </template>

      <template v-slot:is_normal="scope">
        <c-select
          v-model="scope.row.is_normal"
          :options="statusOptions"
          @change="submit(scope.row)"
        ></c-select>
      </template>

      <template v-slot:remark="scope">
        <el-input v-model="scope.row.remark" @blur="submit(scope.row)"></el-input>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import SelectAddress from '@/components/select/SelectAddress'

const SERVICE_NAME = 'credit'

export default {
  components: {
    SelectAddress,
  },
  props: {},
  data() {
    return {
      tableName: 'datum_repay_list_view',
      tableId: '',
      params: {
        index: 1,
        size: 15,
        customer_name: '',
        lender_idcard: '',
        address_code: '',
        pay_time: '',
      },
      loading: false,
      submit_type: '',
      tableData: [],
      tableCount: 0,
      tableColumns: [
        { attrs: { label: '所属地区', prop: 'address', minWidth: '140px' } },
        {
          attrs: { label: '主体名称', prop: 'customer_name', minWidth: '140px' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '经营者姓名', prop: 'corp_name' } },
        { attrs: { label: '身份证号码', prop: 'lender_idcard' } },
        { attrs: { label: '款本付息时间', prop: 'pay_time' } },
        // { attrs: { label: '本月结息', prop: 'isInterest', align: 'center' } },
        // { attrs: { label: '本月还本', prop: 'isCapital', align: 'center' } },
        { attrs: { label: '应还利息', prop: 'interest', align: 'center' }, slot: 'interest' },
        {
          attrs: { label: '应还本金', prop: 'repay_amount', align: 'center' },
          slot: 'repay_amount',
        },
        { attrs: { label: '还本付息情况', prop: 'is_normal', align: 'center' }, slot: 'is_normal' },
        { attrs: { label: '备注', prop: 'remark', align: 'center' }, slot: 'remark' },
      ],

      exportLoading: false,
      statusOptions: {
        1: '正常',
        0: '异常',
      },
    }
  },
  computed: {
    ...mapGetters(['org']),
  },
  watch: {},
  created() {
    let orgClass = this.org.orgClass
    let clientId = this.org.clientId
    this.submit_type =
      orgClass === 'org_gov_ny'
        ? '1'
        : orgClass === 'org_agr_leader'
        ? '3'
        : orgClass === 'org_supply_market'
        ? '4'
        : '2'
    if (
      orgClass === 'org_gov_ny' ||
      orgClass === 'org_agr_leader' ||
      orgClass === 'org_supply_market'
    ) {
      this.params.address_code = this.$utils.getAddressCode()
      this.params['submit_type.eq'] = this.submit_type
    } else if (orgClass === 'org_bank') {
      this.params['bank_id.eq'] = clientId
    } else {
      this.params['orWhere.service_id.eq'] = clientId
      this.params['orWhere.recommend_service.eq'] = clientId
    }
    this.getData()
  },
  mounted() {},
  methods: {
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      const [res] = await this.$utils.promise(
        this.$api.getData(SERVICE_NAME, this.tableName, this.params)
      )
      this.loading = false

      const data = (res && res.data) || {}
      this.tableData = (data.data || []).map(item => {
        item.isCapital = +item.is_capital === 0 ? '未还本' : '已还本'
        item.isInterest = +item.is_interest === 0 ? '未结息' : '已结息'
        return item
      })
      this.tableCount = data.totalCount || 0
    },
    async handleDownload() {
      const { index, size, ...params } = this.params // eslint-disable-line
      const exportParams = {
        service: SERVICE_NAME,
        table: this.tableName,
        params: params,
        filename: '放款到期.xlsx',
      }

      this.exportLoading = true
      await this.$utils.promise(this.$api.exportData(exportParams))
      this.exportLoading = false
    },
    async submit(row) {
      const { datum_repay_list_id: id, ...data } = row
      const [, err] = await this.$utils.promise(
        this.$api.updateData(SERVICE_NAME, 'datum_repay_list', id, {
          interest: data.interest,
          repay_amount: data.repay_amount,
          is_normal: data.is_normal,
          remark: data.remark,
        })
      )
      if (err) {
        this.$message.error(`修改失败（${(err && err.status) || ''}）`)
      }
    },
  },
}
</script>

<style lang="scss" scoped></style>
